<?php
//------------------------
// 类型规格验证器
//-------------------------

namespace app\common\validate;

use think\Validate;

class TypeSpec extends Validate
{
    protected $rule = [
        'spec_name|属性名称'   => 'require|max:32',
        'seq|排序值'           => 'max:10|number',
        'search_index'         => 'number',
        'item|可选值'          => 'max:1024',
    ];

    protected $scene = [
        'add'  => ['spec_name'=>'require|max:32|addCheckName:', 'seq','search_index','item'],
        'edit'  => ['spec_name'=>'require|max:32|editCheckName:', 'seq','search_index','item'],
    ];

    protected function addCheckName($value){
        $res = Db('TypeSpec')->where(array('spec_name' => $value,'type_id' => input('type_id')))->find();
        if($res){
            return '当前类型下已经存在相同名称的规格';
        }
        return true;
    }

    protected function editCheckName($value){
        $TypeSpecModel = model('TypeSpec');

        $old_name = $TypeSpecModel->field('spec_name')->where('id',input('id'))->find();

        if ($value != $old_name['spec_name']) {
            $res = $TypeSpecModel->where(array('spec_name' => $value,'type_id' => input('type_id') ) )->find();
            if($res){
                return '当前类型下已经存在相同名称的规格';
            }
        }

        return true;
    }


}